package myJs.projectCps.uniAnaTab

import cats.effect._
import com.softwaremill.quicklens._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react._
import japgolly.scalajs.react.util.EffectCatsEffect._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.Utils.layer
import myJs.api.Api
import myJs.myPkg.Hooks._
import myJs.myPkg.Swal.swal
import myJs.myPkg._
import myJs.myPkg.reactHookForm.{FormProvider, ReactHookForm, UseFormOptions, yupResolver}
import myJs.myPkg.tabs._
import myJs.projectCps._
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.FeatureSelectionTool
import shared.pojo.MissionTool.CheckFileExist
import shared.pojo.mission
import shared.pojo.mission._
import shared.tool.DefaultPickle
import upickle.default._

import scala.collection.immutable.SeqMap
import scala.scalajs.js
import scala.scalajs.js.{Date, JSON}

/** Created by yz on 3/11/2020
  */
object Cp {

  case class Props(idStr: String = "") {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val tabs = List("volcano", "zScore", "box", "violin")
    val initTab = tabs(0)
    val initTabExistMap = tabs.map(x => (x, true)).toSeqMap
    val (tabExistMap, setTabExistMap) = useState(initTabExistMap)
    val (tab, setTab) = useState(initTab)

    val idStr = props.idStr

    object FTool {

      def tabChange = (v: String) => {
        setTab(v)
      }

      def refreshTabExist(tabName: String, fileNamePrefixIn: String) = {
        val fileName = s"${fileNamePrefixIn}.png"
        val finalFileName = FeatureSelectionTool.getFinalFileName(idStr, fileName)
        val request = CheckFileExist.Request(missionId = Tool.getMissionId, fileName = finalFileName)
        Api.missionTool.checkFileExist(request).flatMapSync { b =>
          setTabExistMap { map =>
            map.updated(tabName, b)
          }
        }
      }

    }

    useEffect(
      {
        FTool.refreshTabExist("zScore", mission.ZScorePlot.pngFileNamePrefixIn)
      },
      List(props.idStr)
    )

    useEffect(
      {
        if (FeatureSelectionTool.isMul) {
          val curTab = if (tabExistMap.isDefinedAt("zScore") && tabExistMap("zScore")) {
            "zScore"
          } else "boxplot"
          setTab(curTab)
        } else SyncIO.unit
      },
      List(props.idStr)
    )

    Tabs(id = "tab", activeKey = tab, onSelect = FTool.tabChange, transition = false)(
      if (!FeatureSelectionTool.isMul) {
        Tab(eventKey = "volcano", title = s"${messages("volcanoPlot")}")(
          div(
            marginTop := "20px",
            volcanoPlot.Cp
              .Props(
                idStr = props.idStr
              )
              .render
          )
        )
      } else EmptyVdom,
      if (tabExistMap("zScore")) {
        Tab(eventKey = "zScore", title = s"${messages("zScorePlot")}")(
          div(
            marginTop := "20px",
            zScorePlot.Cp
              .Props(
                idStr = props.idStr
              )
              .render
          )
        )
      } else EmptyVdom,
      Tab(eventKey = "box", title = s"Box Plot")(
        div(
          marginTop := "20px",
          boxplot.Cp
            .Props(
              idStr = props.idStr
            )
            .render
        )
      ),
      Tab(eventKey = "violin", title = s"Violin Plot")(
        div(
          marginTop := "20px",
          violinPlot.Cp
            .Props(
              idStr = props.idStr
            )
            .render
        )
      )
    )

  }

}
